package com.ncloud.system.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.system.entity.Dictionary;
import com.ncloud.system.entity.RegionData;
import com.ncloud.system.mapper.DictionaryMapper;
import com.ncloud.system.mapper.RegionDataMapper;
import com.ncloud.system.mapper.SequenceMapper;
import com.ncloud.system.service.DictionaryService;
@Service
public class DictionaryServiceImpl implements DictionaryService{
	@Resource
	private DictionaryMapper dictionaryMapper;
	@Resource
	private RegionDataMapper regionDataMapper;
	@Resource
	private SequenceMapper sequenceMapper;

	@Override
	public JSONArray getDictionaryInfo() {
		List<JSONObject> ls = dictionaryMapper.getDictionaryInfo();
		JSONArray results=JSON.parseArray(JSON.toJSONString(ls));
		return results;
	}

	@Override
	public JSONArray getRegionDataInfo(String pid) {
		List<RegionData> ls = regionDataMapper.getRegionDataInfo(pid);
		JSONArray results=JSON.parseArray(JSON.toJSONString(ls));
		return results;
	}

	@Override
	public int insertDictionary(JSONObject json) {
		int num=0;
		String dictionaryId=json.getString("dictionaryId");
		String dictionaryName=json.getString("dictionaryName");
		Long userId=json.getLong("userID");
		JSONArray ja=json.getJSONArray("keys");
		for(int i=0;i<ja.size();i++)
		{
			JSONObject js=ja.getJSONObject(i);
			Dictionary dictionary = new Dictionary();
			dictionary.setDictionaryId(dictionaryId);
			dictionary.setDictionaryName(dictionaryName);
			dictionary.setDictKey(js.getString("dictKey"));
			dictionary.setDictValue(js.getString("dictValue"));
			dictionary.setDictDescribe(js.getString("dictDescribe"));
			dictionary.setOrderNum(js.getInteger("orderNum"));
			dictionary.setCreateUser(userId);
			dictionary.setCreateDate(new Date());
			dictionary.setRemarks("");
			num=num+dictionaryMapper.insertDictionary(dictionary);
		}
		return num;
	}

	@Override
	public int updateDictionary(JSONObject json) {
		int num=0;
		String dictionaryId=json.getString("dictionaryId");
		String dictionaryName=json.getString("dictionaryName");
		Long userId=json.getLong("userID");
		JSONArray ja=json.getJSONArray("keys");
		dictionaryMapper.deleteDictionaryById(dictionaryId);
		for(int i=0;i<ja.size();i++)
		{
			JSONObject js=ja.getJSONObject(i);
			Dictionary dictionary = new Dictionary();
			dictionary.setDictionaryId(dictionaryId);
			dictionary.setDictionaryName(dictionaryName);
			dictionary.setDictKey(js.getString("dictKey"));
			dictionary.setDictValue(js.getString("dictValue"));
			dictionary.setDictDescribe(js.getString("dictDescribe"));
			dictionary.setOrderNum(js.getInteger("orderNum"));
			dictionary.setCreateUser(userId);
			dictionary.setCreateDate(new Date());
			dictionary.setRemarks("");
			num=num+dictionaryMapper.insertDictionary(dictionary);
		}
		return num;
	}

	@Override
	public int deleteDictionaryById(String dictionaryId) {
		return dictionaryMapper.deleteDictionaryById(dictionaryId);
	}
	@Override
	public JSONArray getDictionaryById(String dictionaryId) {
		List<Dictionary> ls = dictionaryMapper.getDictionaryById(dictionaryId);
		JSONArray results=JSON.parseArray(JSON.toJSONString(ls));
		return results;
	}
}
